package org.apache.rocketmq.logging.inner.appenders;

import org.apache.rocketmq.logging.inner.Appender;
import org.apache.rocketmq.logging.inner.Layout;

public class AppenderBuilder {

    public static final String LOGGING_ENCODING = "rocketmq.logging.inner.encoding";

    public static final String ENCODING = System.getProperty(LOGGING_ENCODING, "UTF-8");

    private AsyncAppender asyncAppender;

    private Appender appender = null;

    private AppenderBuilder() {

    }

    public AppenderBuilder withLayout(Layout layout) {
        appender.setLayout(layout);
        return this;
    }

    public AppenderBuilder withName(String name) {
        appender.setName(name);
        return this;
    }

    public AppenderBuilder withConsoleAppender(String target) {
        ConsoleAppender consoleAppender = new ConsoleAppender();
        consoleAppender.setTarget(target);
        consoleAppender.activateOptions();
        this.appender = consoleAppender;
        return this;
    }

    public AppenderBuilder withFileAppender(String file) {
        FileAppender appender = new FileAppender();
        appender.setFile(file);
        appender.setAppend(true);
        appender.setBufferedIO(false);
        appender.setEncoding(ENCODING);
        appender.setImmediateFlush(true);
        appender.activateOptions();
        this.appender = appender;
        return this;
    }

    public AppenderBuilder withRollingFileAppender(String file, String maxFileSize, int maxFileIndex) {
        RollingFileAppender appender = new RollingFileAppender();
        appender.setFile(file);
        appender.setAppend(true);
        appender.setBufferedIO(false);
        appender.setEncoding(ENCODING);
        appender.setImmediateFlush(true);
        appender.setMaximumFileSize(Integer.parseInt(maxFileSize));
        appender.setMaxBackupIndex(maxFileIndex);
        appender.activateOptions();
        this.appender = appender;
        return this;
    }

    public AppenderBuilder withDailyFileRollingAppender(String file, String datePattern) {
        DailyRollingFileAppender appender = new DailyRollingFileAppender();
        appender.setFile(file);
        appender.setAppend(true);
        appender.setBufferedIO(false);
        appender.setEncoding(ENCODING);
        appender.setImmediateFlush(true);
        appender.setDatePattern(datePattern);
        appender.activateOptions();
        this.appender = appender;
        return this;
    }

    public AppenderBuilder withAsync(boolean blocking, int buffSize) {
        AsyncAppender asyncAppender = new AsyncAppender();
        asyncAppender.setBlocking(blocking);
        asyncAppender.setBufferSize(buffSize);
        this.asyncAppender = asyncAppender;
        return this;
    }

    public Appender build() {
        if (appender == null) {
            throw new RuntimeException("please specify appender first");
        }
        if (asyncAppender != null) {
            asyncAppender.addAppender(appender);
            return asyncAppender;
        } else {
            return appender;
        }
    }

    public static AppenderBuilder newAppenderBuilder() {
        return new AppenderBuilder();
    }
}